

let pendingMap = new Map()

export class AxiosCanceler {
  addPending(config) {
    this.removePending(config)
    const url = config.url || ''
    const controller = new AbortController()
    config.signal = controller.signal
    if (!pendingMap.has(url)) {
      pendingMap.set(url, controller)
    }
  }
  removeAllPending() {
    pendingMap.forEach((controller) => {
      controller && typeof controller.abort === 'function' && controller.abort()
    })
    pendingMap.clear()
  }
  removePending(config) {
    const url = config.url || ''
    if (pendingMap.has(url)) {
      const controller = pendingMap.get(url)
      controller && controller.abort()
      pendingMap.delete(url)
    }
  }
  reset() {
    pendingMap = new Map()
  }
}
